const http = require("http");
const path = require("path");
const fs = require("fs");
const url = require("url");
const querystring = require("querystring");
const {getNowTime} = require("./modules/utils");
const hobbyEnum = {
	1:"学习",
	2:"游戏",
	3:"下棋",
	4:"游泳",
	5:"钓鱼"
}

function writeLog(query,res,req){
	let appendContent = "";
	const {userName, sex, isJieHun,hobby} = query;
	if (userName.trim().length > 0) {
		appendContent += "用户名：" + userName.trim() + "\n";
	}
	appendContent += "性别：" + (sex/1 === 1 ? '男' : '女') + "\n";
	if (isJieHun / 1 !== 0){
		appendContent += "是否已婚：" + (isJieHun/1 === 1 ? '已婚' : '未婚') + "\n";
	}
	if(hobby){
		appendContent+="爱好：";
		// 判断是否为数组
		if(hobby instanceof Array){
			appendContent+=hobby.map(key=>hobbyEnum[key]).join("、");
		}else{
			appendContent+=hobbyEnum[hobby];
		}
		appendContent+="\n";
	}
	appendContent+="请求方式："+req.method+"\n";
	appendContent+="提交时间："+getNowTime()+"\n";
	appendContent+="IP："+req.connection.remoteAddress+"\n";
	console.log(query)
	appendContent += "********************************************\n";
	fs.writeFile(path.join(__dirname, "/data/index.log"), appendContent, {flag: "a"}, err => {
		if (err) res.end("收集数据失败");
		else res.end("get收集数据成功")
	})
}


http.createServer((req, res) => {
	const {pathname, query} = url.parse(req.url, true);
	if (pathname === "/") {
		fs.readFile(path.join(__dirname, "./index.html"), (err, result) => {
			res.end(result);
		})
	} else if (pathname === "/info") {
		// console.log(req.method);// 获取请求方式
		// console.log(req.httpVersion);// 协议版本 1.1
		// console.log(req.headers);// 请求头
		// console.log(req.host);// zhangpeiyue.com
		res.setHeader("Content-Type", "text/html;charset=utf-8;");
		if (req.method === "GET") {
			writeLog(query,res,req);
			// let appendContent = "";
			// const {userName, sex, isJieHun,hobby} = query;
			// if (userName.trim().length > 0) {
			// 	appendContent += "用户名：" + userName.trim() + "\n";
			// }
			// appendContent += "性别：" + (sex/1 === 1 ? '男' : '女') + "\n";
			// if (isJieHun / 1 !== 0){
			// 	appendContent += "是否已婚：" + (isJieHun/1 === 1 ? '已婚' : '未婚') + "\n";
			// }
			// if(hobby){
			// 	appendContent+="爱好：";
			// 	// 判断是否为数组
			// 	if(hobby instanceof Array){
			// 		appendContent+=hobby.map(key=>hobbyEnum[key]).join("、");
			// 	}else{
			// 		appendContent+=hobbyEnum[hobby];
			// 	}
			// 	appendContent+="\n";
			// }
			// appendContent+="请求方式："+req.method+"\n";
			// appendContent+="提交时间："+getNowTime()+"\n";
			// appendContent+="IP："+req.connection.remoteAddress+"\n";
			// console.log(query)
			// appendContent += "********************************************\n";
			// fs.writeFile(path.join(__dirname, "/data/index.log"), appendContent, {flag: "a"}, err => {
			// 	if (err) res.end("收集数据失败");
			// 	else res.end("get收集数据成功")
			// })
		} else {
			let info = "";
			// 接收数据
			req.on("data",(chunk)=>{
				// console.log(chunk.length);
				info+=chunk;
			});
			// 接收数据完毕
			req.on("end",()=>{
				writeLog(querystring.parse(info),res,req);
				// let appendContent = "";
				// const {userName, sex, isJieHun,hobby} = querystring.parse(info);
				// if (userName.trim().length > 0) {
				// 	appendContent += "用户名：" + userName.trim() + "\n";
				// }
				// appendContent += "性别：" + (sex/1 === 1 ? '男' : '女') + "\n";
				// if (isJieHun / 1 !== 0){
				// 	appendContent += "是否已婚：" + (isJieHun/1 === 1 ? '已婚' : '未婚') + "\n";
				// }
				// if(hobby){
				// 	appendContent+="爱好：";
				// 	// 判断是否为数组
				// 	if(hobby instanceof Array){
				// 		appendContent+=hobby.map(key=>hobbyEnum[key]).join("、");
				// 	}else{
				// 		appendContent+=hobbyEnum[hobby];
				// 	}
				// 	appendContent+="\n";
				// }
				// appendContent+="请求方式："+req.method+"\n";
				// appendContent+="提交时间："+getNowTime()+"\n";
				// appendContent+="IP："+req.connection.remoteAddress+"\n";
				// console.log(query)
				// appendContent += "********************************************\n";
				// fs.writeFile(path.join(__dirname, "/data/index.log"), appendContent, {flag: "a"}, err => {
				// 	if (err) res.end("收集数据失败");
				// 	else res.end("get收集数据成功")
				// })
			})
			res.end("post成功！")
		}
		
	} else {
		res.end("404");
	}
}).listen(80,"0.0.0.0", () => {
	console.log("success")
})